local bocai = fk.CreateSkill {
  name = "cf_bocai",
}

Fk:loadTranslationTable {
  ["cf_bocai"] = "博才",
  [":cf_bocai"] = "准备阶段，你可以弃置一张牌，获得两张锦囊牌，若该锦囊牌不为“书”，将其标记为“书”。出牌阶段，你可以将一张“书”交给一名本回合未以此法交给过的角色。",
  ["@@cf_bocai-inhand"] = "书",
  ["#cf_bocai-target"] = "博才：你可以弃置一张牌，获得两张锦囊牌，标记为“书”",
  ["#cf_bocai-target-support"] = "博才：你可以将一张“书”交给一名本回合未以此法交给过的角色",
  ["$cf_bocai1"] = "快点投降吧！",
  ["$cf_bocai2"] = "让姐姐帮帮你吧！",
}

bocai:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bocai.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = bocai.name,
          prompt = "#cf_bocai-target",
          cancelable = true
        }) ~= 0 then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getCardsFromPileByRule(".|.|.|.|.|trick", 2, "allPiles")
    for _, card in ipairs(cards) do
      if Fk:getCardById(card):getMark(bocai.name) < 1 then
        room:setCardMark(Fk:getCardById(card), bocai.name, 1)
        room:setCardMark(Fk:getCardById(card), "@@cf_bocai-inhand", 1)
      end
    end
    player:broadcastSkillInvoke(bocai.name, 1)
    room:notifySkillInvoked(player, bocai.name)
    room:moveCardTo(cards, Player.Hand, player, fk.ReasonJustMove, bocai.name, nil, false, player)
  end
})

bocai:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return not player:isKongcheng()
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(player:getCardIds("h")) do
      local card = Fk:getCardById(id)
      room:setCardMark(card, "@@cf_bocai-inhand", (card:getMark(bocai.name) > 0 and 1 or 0))
    end
  end
})

bocai:addEffect("active", {
  mute = true,
  anim_type = "support",
  prompt = "#cf_bocai-target-support",
  min_card_num = 1,
  max_card_num = 1,
  target_num = 1,
  card_filter = function(self, player, to_select, selected, selected_targets)
    return Fk:getCardById(to_select):getMark(bocai.name) ~= 0 and table.contains(player:getCardIds("h"), to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0 and to_select ~= player and not table.contains(player:getTableMark("cf_bocai-phase"), to_select
      .id)
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    local card = skillUseEvent.cards[1]
    local target = skillUseEvent.tos[1]
    room:moveCardTo(card, Player.Hand, target, fk.ReasonJustMove, bocai.name, nil, false, player)
    player:broadcastSkillInvoke(bocai.name, 2)
    room:notifySkillInvoked(player, bocai.name)
    room:addTableMark(player, "cf_bocai-phase", target.id)
  end
})

return bocai
